package com.iaiai.calendar.activity;

import java.text.ParseException;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iaiai.calendar.util.DateUtil;
import com.iaiai.calendar.util.LunarCalendar;
import com.iaiai.calendar.util.SolarTerms;

/**
 * 
 * <p>
 * Title: DayActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-1-21
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class DayActivity extends Activity {

	private TextView dayTextView; // 阳历
	private TextView lunarDayTextView; // 阴历
	private TextView shuxingTextView; // 生肖
	private TextView constellationTextView; // 星座
	
	private LinearLayout layout;

	private Bundle bundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.day);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

		init();
	}

	private void init() {
		setTitle(R.string.app_title);
		
		bundle = getIntent().getExtras();
		layout = (LinearLayout) findViewById(R.id.layout);
		dayTextView = (TextView) findViewById(R.id.day);
		lunarDayTextView = (TextView) findViewById(R.id.lunarDay);
		shuxingTextView = (TextView) findViewById(R.id.shuxing);
		constellationTextView = (TextView) findViewById(R.id.constellation);

		try {
			LunarCalendar lunar = new LunarCalendar(DateUtil.formatDate(bundle
					.getString("day")));
			dayTextView.setText(DateUtil.formatDate(DateUtil.formatDate(bundle
					.getString("day"))));
			lunarDayTextView.setText("农历:" + lunar.getDateInfo());
			shuxingTextView.setText("属性:" + sxiao(lunar));
			constellationTextView.setText("星座:"
					+ DateUtil.getConstellation(DateUtil.formatDate(bundle
							.getString("day"))));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	/**
	 * 计算生肖
	 * 
	 * @param lunarCalendar
	 *            农历
	 * @return
	 */
	private String sxiao(LunarCalendar lunarCalendar) {
		int year = DateUtil.getYear();
		if (year > lunarCalendar.getYear()) { // 如果阳历是新的一年，但阴历还没过春节，这时阴历年是比阳历年小
			return DateUtil.getShengxiao(lunarCalendar.getYear());
		}
		try {
			// 获得立春(阳历)，立春肯定是在2月
			String lc = year
					+ "-2-"
					+ SolarTerms.getSolarTermNum(year,
							SolarTerms.SolarTermsEnum.LICHUN.name());
			long lctime = DateUtil.formatDate(
					new LunarCalendar(DateUtil.formatDate(lc)).getDate())
					.getTime();
			if (DateUtil.formatDate(lunarCalendar.getDate()).getTime() < lctime) {
				return DateUtil.getShengxiao(year - 1);
			} else {
				return DateUtil.getShengxiao(year);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	public boolean OnKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
		}
		return false;
	}

}
